﻿using AzukiMap.Layer;
using System.Collections.Generic;
using AzukiMap.MapSource;
using Geotools.IO;
using AzukiMap.Map;
using Geotools.Geometries;
using System.Windows.Threading;
using System;
using AzukiMap.Tools.Logger;
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("TestAzukiMapSL.Request")]
namespace AzukiMap.Request
{
    public class PostGisVectorRequest : ServerRequest
    {
        const string EXPRESSION_SEP_CHAR = "<->";

        protected IMapStatus _mapCanvas;
        protected IVectorLayer _vectorLayer;
        protected string _currentExtentWkt;
        protected string _previousExtentWkt;
        protected PostGisSource _table;
        protected List<string> _expressions;
        protected Dictionary<string, string> _params;
        protected GeometryWKTWriter _wktWriter;
        protected int _srid;
        protected double _tolerance;

        public IMapStatus MapCanvas
        {
            get { return _mapCanvas; }
            set { 
                _mapCanvas = value;
                SetMapParams();
            }
        }

        public IVectorLayer VectorLayer
        {
            get { return _vectorLayer; }
            set { 
                _vectorLayer = value;
                SetLayerParams();
            }
        }

        public PostGisVectorRequest(IMapStatus mapCanvas, IVectorLayer vectorLayer)
            : base(AzukiMapSettings.DEFAULT_GETPOSTGISDATA_URI)
        {
            Logger.Debug(
                "PostGisVectorRequest.PostGisDataRequest(2):"
                + " vectorLayer.Name = " + vectorLayer.Name
                );

            init(mapCanvas, vectorLayer);
        }

        public PostGisVectorRequest(IMapStatus mapCanvas, IVectorLayer vectorLayer, string getColumnDataUri)
            : base(getColumnDataUri)
        {
            Logger.Debug(
                "PostGisVectorRequest.PostGisDataRequest(3):"
                + " vectorLayer.Name = " + vectorLayer.Name
                );

            init(mapCanvas, vectorLayer);
        }

        protected void init(IMapStatus mapCanvas, IVectorLayer vectorLayer)
        {
            _wktWriter = new GeometryWKTWriter();
            _params = new Dictionary<string, string>();

            MapCanvas = mapCanvas;
            VectorLayer = vectorLayer;
        }

        protected void SetMapParams()
        {
            if (_mapCanvas != null)
            {
                _srid = _mapCanvas.Srid;
                _tolerance = _mapCanvas.Tolerance;
                _currentExtentWkt = _wktWriter.Write(_mapCanvas.CurrentExtent);
                //_previousExtentWkt = _wktWriter.Write(_mapCanvas.LastExtent);
                if (_mapCanvas.RenderedExtent != null)
                {
                    _previousExtentWkt = _wktWriter.Write(_mapCanvas.RenderedExtent);
                }
                else
                {
                    _previousExtentWkt = "";
                }
            }
        }

        protected void SetLayerParams()
        {
            if (_vectorLayer != null)
            {
                _table = (PostGisSource)_vectorLayer.Data;
                _expressions = _vectorLayer.Expressions;
            }
        }


        internal override void SetRequestParams()
        {
            SetMapParams();
            SetLayerParams();
            try
            {
                string expressions = CreateExpressionString();
                _params["table"] = _table.TableName;
                _params["column"] = _table.GeometryColumn;
                _params["db"] = _table.DbName;
                _params["cr"] = _currentExtentWkt;
                _params["pr"] = _previousExtentWkt;
                _params["exs"] = expressions;
                _params["tolerance"] = _tolerance.ToString();
                _params["srid"] = _srid.ToString();
                _params["tsrid"] = _table.Srid.ToString();
            }
            catch (ArgumentNullException ex)
            {
                Logger.Error("MapStatus or LayerStatus may be null.", ex);
            }
            catch (Exception ex)
            {
                Logger.Error("Something wrong to create a request params.", ex);
            }

            RequestParams = _params;

            Logger.Debug(
                "PostGisDataRequest.SetRequestParams(): "
                + " RequstParams = " + RequestParams.ToString()
                );
        }

        string CreateExpressionString()
        {
            string exprStr = "";
            foreach (string expr in _expressions)
            {
                exprStr += expr + EXPRESSION_SEP_CHAR;
            }
            return exprStr;
        }


    }
}
